import QtQuick 1.1
import com.nokia.meego 1.0
import REngine 1.0
import "Common.js" as COM

RPage {
    id: recentView

    property int column: 2
    property int row:2
    property string longPressFileName: ""
    property color backgroundColor: "white"
    property color textColor: "black"
    property string textFontFamily: "Arail"
    property Item pressedThumbnail: null

    function stripFilePathPrefix(path) {
        console.log("stripFilePathPrefix():path:", path)
        var filePath = new String(path)
        return filePath.substr(filePath.lastIndexOf("/")+1).replace(".txt","").replace(".umd","")
    }

    function toFixed(value, precision) {
        var number = new Number(value)
        return number.toFixed(precision)
    }

    function reload() {
        console.log("reload")
        screen.allowedOrientations = engine.setting(REngine.Orientation)
        backgroundColor = engine.setting(REngine.TextBackgroud)
        textColor = engine.setting(REngine.TextColor)
        textFontFamily = engine.setting(REngine.TextFontFamiliy)
        gridView.model = engine.recentFileModel()
    }

    function fileCount() {
        if (gridView.model != null) {
            console.log("model is not null, count", gridView.model.count())
            return gridView.model.count()
        }
        else {
            console.log("model is null, return 0")
            return 0
        }
    }


    Rectangle {
        anchors.top: parent.top
        anchors.bottom: parent.bottom
        anchors.left: parent.left
        anchors.right: parent.right
        color: "black"

        Text {
            anchors.centerIn: parent
            text: qsTr("No File Opened")
            color: "azure"
            font.family: "Arial"
            font.pixelSize: 60
            z: 1
            opacity: fileCount() > 0 ? 0 : 0.4
        }

        GridView {
            id: gridView
            property int margin: 5
            anchors.fill: parent
            cacheBuffer: cellHeight*8
//            model: engine.recentFileModel()
            cellWidth: Math.floor(gridView.width/recentView.column)
            cellHeight: Math.floor(gridView.height/recentView.row)
            delegate: Thumbnail {
                id: thumbnail
                fileName: model.fileName
                width: gridView.cellWidth - gridView.margin
                height: gridView.cellHeight - gridView.margin
                startLine: lastReadLine
                startChar: lastReadCharIndex
                viewFontSize: 18
                viewFontColor: recentView.textColor
                viewFontFamily: recentView.textFontFamily
                color: recentView.backgroundColor
                titleText: stripFilePathPrefix(model.fileName)
                info1Text: engine.epocToString(lastReadTime)
                info2Text: toFixed((lastReadLine/lineCount)*100, 2)+"%"
                onClicked: {
                    console.log("file", model.fileName, " is clicked, open it")
                    recentView.pageStack.push(Qt.resolvedUrl("Viewer.qml"),
                                                {fileName: model.fileName,
                                                    window: recentView.window/*,
                                                    orientationLock: recentView.orientationLock*/})
                }
                onPressAndHold: {
                    console.log("file", model.fileName, " is longpressed, open context menu")
                    recentView.longPressFileName = model.fileName
                    longPressMenu.pressedThumbnail = thumbnail
                    longPressMenu.open()
                }
            }
        }
        Component.onCompleted: {
            console.log("recentView loaded, width", recentView.width, "height", recentView.height,
                        "gridView.width",gridView.width, "gridView.height",gridView.height)
        }
    }
    tools: ToolBarLayout {
        ToolIcon {
            id: menuTool
            iconId: "toolbar-view-menu"
            onClicked: {
                console.log("menu clicked")
                orientationMenu.open()
            }
        }
    }
    onStatusChanged: {
        console.log("status changed:", status)
        if (status == PageStatus.Active) {
            console.log("reload model")
//            COM.disableDisplayBlank(true)
            reload()
        }else if (status == PageStatus.Activating) {
            screen.allowedOrientations = engine.setting(REngine.Orientation)
        }
        else if (status == PageStatus.Deactivating) {
//            COM.disableDisplayBlank(false)
        }
    }

    Connections {
        target: screen
        onCurrentOrientationChanged: {
            console.log("orientation Changed, reload model")
            if (status == PageStatus.Active) {
                reload()
            }
        }
    }

//    Component.onCompleted: {
//        reload()
//    }

    ContextMenu {
        id: longPressMenu
        property Item pressedThumbnail: null
        MenuLayout {
            MenuItem {
                text: qsTr("Close File")
                onClicked: {
                    console.log("Remove menu clicked")
                    engine.removeRecentFileInfo(recentView.longPressFileName)
                }
            }
        }
        onStatusChanged: {
            if (status == DialogStatus.Closing) {
                if (longPressMenu.pressedThumbnail != null) {
                    longPressMenu.pressedThumbnail.state = "Normal"
                    longPressMenu.pressedThumbnail = null
                }
            }
        }
    }

    ContextMenu {
        id: orientationMenu
        MenuLayout {
            id: orientationMenuLayout
            MenuItem {
                text: qsTr("Open File")
                onClicked: {
                    recentView.pageStack.push(Qt.resolvedUrl("FileListView.qml"),
                                              {  window: recentView.window,
                                                  orientationLock: recentView.orientationLock})
                }
            }
            MenuItem {
                text: qsTr("Close All Files")
                onClicked: {
                    console.log("remove all menu clicked")
                    engine.removeAllRecentFileInfos()
                }
            }
            MenuItem {
                text: qsTr("Setting")
                onClicked: {
                    console.log("setting menu clicked")
                    recentView.pageStack.push(Qt.resolvedUrl("SettingView.qml"),
                                              {  window: recentView.window,
                                                  orientationLock: recentView.orientationLock})
                }
            }
            MenuItem {
                text: qsTr("Help")
                onClicked: {
                    console.log("Help menu clicked")
                    help.open()
                }
            }
            MenuItem {
                text: qsTr("About")
                onClicked: {
                    console.log("About menu clicked")
                    about.open()
                }
            }
        }
    }

    About {
        id: about
    }

    Help {
        id: help
    }

    Connections {
        target: engine
        onRecentFileInfoChanged: reload()
    }

//    Connections {
//        target: platformWindow
//        onViewModeChanged: {
//            console.log("viewModeChanged", platformWindow.viewMode)
//            COM.handleViewModeVisibleChanged()
//        }
//        onVisibleChanged: {
//            console.log("visibleChanged", platformWindow.visible)
//            COM.handleViewModeVisibleChanged()
//        }
//    }
}
